/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.neuralnetwork;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author afspear
 */
public class Perceptron {
    //not outputs, but this the collection of data and weights coming form the layer under this perceptron to this perceptron

    public ArrayList<Input> inputs = new ArrayList<Input>();
    public ArrayList<Input> outputs = new ArrayList<Input>();
    public Layer layer;
    public double threshold;
    Function function;
    public double output;
    //unique ID in layer
    public int ID;
    //error computed by backpropagation error
    public double error;

    public void getInput() {
        for (Input input : inputs) {
            if (input.data == Double.NaN) {
                input.data = input.fromPerceptron.output;
            }
        }
    }
    
    

    public void computeOutput() {
        this.function = new Function(this.threshold);
        this.output = function.logisticFunction(inputs);
        for(Input output : this.outputs)
            {
                output.data = this.output;
        }
        if (layer.name == LayerName.Output) {
            //System.out.println("Perceptron ID:" + ID + "\tLayer:" + layer.name + "\tOutput:" + this.output);
        }
    }

    public void addInputs(Input input) {
        this.inputs.add(input);
    }

    public void addOutputs(Input output) {
        this.outputs.add(output);
    }

    public Perceptron() {
        this.output = 0;

    }

    public Perceptron(double output) {
        this.output = output;
    }

    public Perceptron(Layer layer, int id) {
        Random random = new Random();
        double number;
        if (random.nextDouble() > .5) {
            number = random.nextDouble();
        } else {
            number = -random.nextDouble();
        }

        this.output = 0;
        this.ID = id;
        this.layer = layer;
        this.threshold = number;

    }
}

